﻿using Common;
using Microsoft.VisualBasic;
using OfficeOpenXml.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewDevice.RS232
{
    public class RK9310 : SerialBase
    {
        private double _testTime;

        public RK9310(string deviceName = "RK9310") : base(deviceName)
        {
        }

        public void AcInit()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// DC 模式 初始化参数
        /// </summary>
        /// <param name="voltage">电压(KV)</param>
        /// <param name="time">时间(s)</param>
        /// <param name="uplm">电流上限(mA)</param>
        /// <param name="dnlm">电流下限(mA)</param>
        /// <param name="rtim">上升时间(s)</param>
        /// <param name="ftim">下降时间(s)</param>
        public void DcInit(double voltage, double time, double uplm, double dnlm = 0, double rtim = 0, double ftim = 0)
        {
            _testTime = time;
            string[] cmds =
            {
                 $"FUNC:SOUR:STEP:CLEA",
                 $"FUNC:SOUR:STEP1:MODE 2",
                 $"FUNC:SOUR:STEP1:MODE:DC:VOLT {voltage}",
                 $"FUNC:SOUR:STEP1:MODE:DC:TTIM {time}",
                 $"FUNC:SOUR:STEP1:MODE:DC:UPLM {uplm}",
                 $"FUNC:SOUR:STEP1:MODE:DC:DNLM {dnlm}",
                 $"FUNC:SOUR:STEP1:MODE:DC:RTIM {rtim}",
                 $"FUNC:SOUR:STEP1:MODE:DC:FTIM {ftim}",
            };
            foreach (string cmd in cmds)
            {
                SendAscii(cmd + "\n");
                Thread.Sleep(100);
            }
        }

        /// <summary>
        /// IR 模式 初始化参数
        /// </summary>
        /// <param name="voltage">电压(KV)</param>
        /// <param name="time">时间(s)</param>
        /// <param name="uplm">电阻上限(MΩ)</param>
        /// <param name="dnlm">电阻下限(MΩ)</param>
        /// <param name="rtim">上升时间(s)</param>
        /// <param name="ftim">下降时间(s)</param>
        public void IrInit(double voltage, double time, double uplm, double dnlm, double rtim = 0, double ftim = 0)
        {
            _testTime = time;
            string[] cmds =
            {
                 $"FUNC:SOUR:STEP:CLEA",
                 $"FUNC:SOUR:STEP1:MODE 3",
                 $"FUNC:SOUR:STEP1:MODE:IR:VOLT {voltage}",
                 $"FUNC:SOUR:STEP1:MODE:IR:TTIM {time}",
                 $"FUNC:SOUR:STEP1:MODE:IR:UPLM {uplm}",
                 $"FUNC:SOUR:STEP1:MODE:IR:DNLM {dnlm}",
                 $"FUNC:SOUR:STEP1:MODE:IR:RTIM {rtim}",
                 $"FUNC:SOUR:STEP1:MODE:IR:FTIM {ftim}",
            };
            foreach (string cmd in cmds)
            {
                SendAscii(cmd + "\n");
                Thread.Sleep(100);
            }
        }

        public (string, bool) StartTest(CancellationToken ct = default)
        {
            for (int i = 0; i < 2; i++)
            {
                var stopCmd = "FUNC:STOP";
                SendAscii(stopCmd + "\n");
                Thread.Sleep(200);
            }

            string cmd = "FUNC:STAR";
            SendAscii(cmd + "\n");
            Thread.Sleep(100);
            //var bytesData = Receive(ReceivedDataVerify, TimeSpan.FromSeconds(3), ct);
            var bytesData = Receive(ReceivedDataVerify, TimeSpan.FromSeconds(_testTime + 10), ct);
            var strData = Encoding.UTF8.GetString(bytesData);   //1,DC,0.800KV,0.5000mA,OFF,0.0002mA,PASS
            var listData = strData.Split(',').ToArray();
            if (listData.Count() == 7)
            {
                return (listData[5], listData[6].Contains("PASS"));
            }
            throw new Exception($"{DeviceName}测试返回数据异常{strData}");
        }

        private bool ReceivedDataVerify(byte[] arg)
        {
            if (arg == null)
                return false;
            BitConverter.ToString(arg);
            var strData = Encoding.UTF8.GetString(arg);
            MyLog.DeviceData(deviceName: DeviceName, parsedValue: strData);
            var ret = strData.Contains("PASS") || strData.Contains("FAIL");
            if (ret)
            {
                MyLog.Debug(message: $"数据校验成功;[{strData}]");
            }
            return ret;
        }
    }
}

//*IDN ?
//FUNC : STAR
//FUNC: SOUR: STEP?  //查询step count
//FUNC:SOUR: STEP: CLEA		//delete steps remain 1 steps
//DISP:PAGE? get current page index
//DISP:PAGE 1		switch to test page

//FUNC:SOUR: STEP1: MODE?	//AC=1 DC=2 IR=3

//FUNC:SOUR: STEP1: MODE: DC: VOLT?
//FUNC:SOUR: STEP1: MODE: DC: VOLT 0.5	//kV

//FUNC: SOUR: STEP1: MODE: DC: UPLM?
//FUNC:SOUR: STEP1: MODE: DC: UPLM 0.5 		//mA

//FUNC: SOUR: STEP1: MODE: DC: TTIM?
//FUNC:SOUR: STEP1: MODE: DC: TTIM 60	//s

//FUNC: SOUR: STEP1: MODE: DC: RTIM?	//dc 上升时间
//FUNC:SOUR: STEP1: MODE: DC: RTIM 0

//FUNC: SOUR: STEP1: MODE: DC: FTIM?	//dc 下降时间
//FUNC:SOUR: STEP1: MODE: DC: FTIM 0

//---- -
//FUNC:SOUR: STEP1: MODE: IR: VOLT?
//FUNC:SOUR: STEP1: MODE: IR: VOLT 0.5	//kV

//FUNC: SOUR: STEP1: MODE: IR: UPLM?
//FUNC:SOUR: STEP1: MODE: IR: UPLM 20 		//MΩ
//FUNC: SOUR: STEP1: MODE: IR: DNLM?
//FUNC:SOUR: STEP1: MODE: IR: DNLM 20 		//MΩ

//FUNC: SOUR: STEP1: MODE: IR: TTIM?
//FUNC:SOUR: STEP1: MODE: IR: TTIM 60	//s

//FUNC: SOUR: STEP1: MODE: IR: RTIM?	//dc 上升时间
//FUNC:SOUR: STEP1: MODE: IR: RTIM 0

//FUNC: SOUR: STEP1: MODE: IR: FTIM?	//dc 下降时间
//FUNC:SOUR: STEP1: MODE: IR: FTIM 0

//------------------------